package com.csc.service;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csc.bean.BlogLogonBean;
import com.csc.bean.NewMember;
import com.csc.dao.SystemAccountDAO;
import com.csc.dao.SystemAccountTypeDAO;
import com.csc.domain.SystemAccount;
import com.csc.domain.SystemAccountType;
import com.csc.domain.SystemStatus;
import com.csc.domain.SystemTimeUsed;
import com.csc.domain.TransactionHistory;

@Service
public class SystemAccountServiceImp implements SystemAccountService {
	@Autowired
	private SystemAccountDAO sysAccDAO;
	@Autowired
	private SystemAccountTypeDAO sysAccTypeDAO;

	public List<SystemAccount> getAccounts() {
		return sysAccDAO.getAccounts();
	}

	public List<SystemAccountType> getAccountsType() {
		return sysAccTypeDAO.getAccountsType();
	}

	public List<SystemAccount> getAccountsByUsername(String userName) {
		return sysAccDAO.getAccountsByUsername(userName);
	}

	public boolean createNewAccount(SystemAccount acc, int accType,
			int accStatus) {
		return sysAccDAO.createNewAccount(acc, accType, accStatus);
	}

	public boolean updatePassword(String username, String password) {
		String encodePassword = PasswordController.encodePassword(password);
		return sysAccDAO.updatePassword(username, encodePassword);
	}

	public boolean updateAdminName(String username, String fName, String lName,
			String mName) {
		return sysAccDAO.updateAdminName(username, fName, lName, mName);
	}

	public boolean updateAdminIdCardNumber(String username, String idCardNo) {
		return sysAccDAO.updateAdminIdCardNumber(username, idCardNo);
	}

	public boolean updateAdminPhone(String username, String phone1,
			String phone2) {
		return sysAccDAO.updateAdminPhone(username, phone1, phone2);
	}

	public boolean updateAdminEmail(String username, String email1,
			String email2) {
		return sysAccDAO.updateAdminEmail(username, email1, email2);
	}

	public boolean updateAdminAddress(String username, String add1, String add2) {
		return sysAccDAO.updateAdminAddress(username, add1, add2);
	}

	public List<SystemAccount> getListAccountActive() {
		return sysAccDAO.getListAccountActive();
	}

	// Count number all type account system and put to Map
	public Map<String, Integer> countNumAccountSystem() {
		Map<String, Integer> map = new Hashtable<String, Integer>();
		map.put("User Admin", sysAccDAO.countNumUserAdmin());
		map.put("User Support", sysAccDAO.countNumUserSupport());
		map.put("Account Admin", sysAccDAO.countNumAccountAdmin());
		map.put("Account Support", sysAccDAO.countNumAccountSupport());
		map.put("Report Support", sysAccDAO.countNumReportSupport());
		return map;
	}

	public SystemAccount getAccountByLoginId(String loginid) {
		return sysAccDAO.getAccountByLoginId(loginid);
	}

	// Truong Le Bao
	@Override
	public List<SystemAccount> getRegisteredAccounts(String username) {
		// TODO Auto-generated method stub
		return sysAccDAO.getRegisteredAccounts(username);
	}

	public SystemAccount getAccountByUsername(String userName) {
		return sysAccDAO.getAccountByUsername(userName);
	}

	/**
	 * @author vuhuynh
	 */
	public boolean doActiveAccountByAdmin(TransactionHistory trans,
			String accAff, int stt) {
		return sysAccDAO.doActiveAccountByAdmin(trans, accAff, stt);
	}

	public boolean doDisableAccountByAdmin(TransactionHistory trans,
			String accAff, int stt) {
		return sysAccDAO.doActiveAccountByAdmin(trans, accAff, stt);
	}

	/* @author: Tran Son Hoang */
	@Override
	public List<TransactionHistory> getListAdminNewUsers() {
		// TODO Auto-generated method stub
		return sysAccDAO.getListAdminNewUsers();
	}

	@Override
	public List<TransactionHistory> searchNewUserRegistrationByAdvanced(
			String name, String LoginId, String IdCardNumber, String Phone,
			String address, String Email, String role) {
		// TODO Auto-generated method stub
		return sysAccDAO.searchNewUserRegistrationByAdvanced(name, LoginId,
				IdCardNumber, Phone, address, Email, role);
	}

	@Override
	public boolean ApproveNewUserInAdminPage(
			TransactionHistory transactionHistory, String AccAffect, int status) {
		// TODO Auto-generated method stub
		return sysAccDAO.ApproveNewUserInAdminPage(transactionHistory,
				AccAffect, status);
	}

	@Override
	public boolean RejectNewUserInAdminPage(
			TransactionHistory transactionHistory, String AccAffect, int status) {
		// TODO Auto-generated method stub
		return sysAccDAO.RejectNewUserInAdminPage(transactionHistory,
				AccAffect, status);
	}

	@Override
	public boolean createNewSystemAccount(String accountLoginId,
			String accountIDCardNumber, String accountFirstName,
			String accountLastName, String accountMidName,
			String accountPhone1, String accountPhone2, String accountEmail1,
			String accountEmail2, String accountAddress1,
			String accountAddress2, SystemAccountType role, SystemStatus stt) {
		// TODO Auto-generated method stub
		return sysAccDAO.createNewSystemAccount(accountLoginId,
				accountIDCardNumber, accountFirstName, accountLastName,
				accountMidName, accountPhone1, accountPhone2, accountEmail1,
				accountEmail2, accountAddress1, accountAddress2, role, stt);
	}

	@Override
	public boolean createNewTransactionHistory(TransactionHistory trans) {
		// TODO Auto-generated method stub
		return sysAccDAO.createNewTransactionHistory(trans);
	}

	@Override
	public List<NewMember> parseToNewUsers(List<TransactionHistory> listAll) {
		// TODO Auto-generated method stub
		List<NewMember> listNewMember = new ArrayList<NewMember>();
		String username = "";
		String fullname = "";
		String role = "";
		String date = "";
		for (TransactionHistory trans : listAll) {
			username = trans.getAccAffected().getAccountLoginId();
			fullname = trans.getAccAffected().getAccountFirstName() + " "
					+ trans.getAccAffected().getAccountMidName() + " "
					+ trans.getAccAffected().getAccountLastName();
			role = trans.getAccAffected().getSystemAccountType().getTypeName();
			date = trans.getDateTransaction();

			/*
			 * listBlogLogon.add(new BlogLogonBean(userName, fullName, role,
			 * task, time));
			 */
			listNewMember.add(new NewMember(username, fullname, role, date));
		}
		return listNewMember;
	}

	@Override
	public List<TransactionHistory> searchNewUserRegistrationByName(String name) {
		// TODO Auto-generated method stub
		List<TransactionHistory> ListAll = sysAccDAO.getListAdminNewUsers();
		List<TransactionHistory> searchResult = new ArrayList<TransactionHistory>();

		for (TransactionHistory trans : ListAll) {
			String FullName = trans.getAccAffected().getAccountFirstName()
					+ " " + trans.getAccAffected().getAccountMidName() + " "
					+ trans.getAccAffected().getAccountLastName();
			if(FullName.contains(name)){
				searchResult.add(trans);
			}
		}
		return searchResult;
	}
	/* end transonhoang */
	
	//Truong Le Bao
	@Override
	public List<SystemAccount> searchAdvanceRegisteredAccounts(String fullName, String username, String idcard, String phone, String address, String email, String role, String status,String loginID) {
		// TODO Auto-generated method stub
		return sysAccDAO.searchAdvanceRegisteredAccounts(fullName, username, idcard, phone, address, email, role, status,loginID );
	}

	@Override
	public SystemAccount getNewUserDetail(String loginId) {
		// TODO Auto-generated method stub
		return sysAccDAO.getNewUserDetail(loginId);
	}
}
